package com.project.movie.controller.address;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieAddress;
import com.project.movie.domain.MovieCinema;
import com.project.movie.domain.MovieItem;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieAddressService;
import com.project.movie.service.MovieCinemaService;
import com.project.movie.service.MovieItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 行政区controller
 * @date 2025/02/06 10:53
 */
@Controller
@ResponseBody
@RequestMapping("address")
public class MovieAddressController {

    @Autowired
    private MovieAddressService movieAddressService;
    @Autowired
    private MovieCinemaService movieCinemaService;

    /** 分页获取行政区 */
    @PostMapping("getMovieAddressPage")
    public Result getMovieAddressPage(@RequestBody MovieAddress movieAddress) {
        Page<MovieAddress> page = new Page<>(movieAddress.getPageNumber(),movieAddress.getPageSize());
        QueryWrapper<MovieAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieAddress.getProvince()),MovieAddress::getProvince,movieAddress.getProvince())
                .like(StringUtils.isNotBlank(movieAddress.getCity()),MovieAddress::getCity,movieAddress.getCity())
                .like(StringUtils.isNotBlank(movieAddress.getCounty()),MovieAddress::getCounty,movieAddress.getCounty());
        Page<MovieAddress> movieAddressPage = movieAddressService.page(page, queryWrapper);
        return Result.success(movieAddressPage);
    }

    @GetMapping("getAddressGroupCity")
    public Result getAddressGroupCity() {
        QueryWrapper<MovieAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().groupBy(MovieAddress::getCity);
        List<MovieAddress> addressList = movieAddressService.list(queryWrapper);
        return Result.success(addressList);
    }

    @GetMapping("getAddressByCity")
    public Result getAddressByCity(@RequestParam("city")String city) {
        QueryWrapper<MovieAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieAddress::getCity,city);
        List<MovieAddress> addressList = movieAddressService.list(queryWrapper);
        return Result.success(addressList);
    }

    @GetMapping("getMovieAddressList")
    public Result getMovieAddressList() {
        List<MovieAddress> addressList = movieAddressService.list();
        return Result.success(addressList);
    }

    /** 根据id获取行政区 */
    @GetMapping("getMovieAddressById")
    public Result getMovieAddressById(@RequestParam("id")String id) {
        MovieAddress movieAddress = movieAddressService.getById(id);
        return Result.success(movieAddress);
    }

    /** 保存行政区 */
    @PostMapping("saveMovieAddress")
    public Result saveMovieAddress(@RequestBody MovieAddress movieAddress) {
        boolean save = movieAddressService.save(movieAddress);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑行政区 */
    @PostMapping("editMovieAddress")
    public Result editMovieAddress(@RequestBody MovieAddress movieAddress) {
        boolean save = movieAddressService.updateById(movieAddress);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除行政区 */
    @GetMapping("removeMovieAddress")
    public Result removeMovieAddress(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                MovieAddress address = movieAddressService.getById(id);
                QueryWrapper<MovieCinema> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(MovieCinema::getCity,address.getCity())
                                .eq(MovieCinema::getCounty,address.getCounty());
                int count = movieCinemaService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("行政区下存在影院无法删除");
                }
                movieAddressService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("行政区id不能为空！");
        }
    }

}
